package com.caishi.lkx.distribution.api;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.caishi.lkx.common.context.UserContext;
import com.caishi.lkx.distribution.model.DistributionMoneyLevel;
import com.caishi.lkx.distribution.service.IMoneyLevelService;
import com.zzw.common.annotation.ApiResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/distribute/money-level")
public class MoneyLevelController {
    private final IMoneyLevelService moneyLevelService;
    @Resource
    private UserContext userContext;

    public MoneyLevelController(IMoneyLevelService moneyLevelService) {
        this.moneyLevelService = moneyLevelService;
    }

    @GetMapping("/list")
    @ApiResult
    public List<DistributionMoneyLevel> list() {
        List<DistributionMoneyLevel> list = moneyLevelService.list();
        return list.stream().peek(item -> {
            item.setLevel(item.getType().getDesc());
            item.setCommissionRate(item.getCommissionRate()*100);
        }).toList();
//        return list;
    }
    @ApiResult
    @PutMapping("/{id}")
    public boolean updateRate(@PathVariable("id") Integer id,@RequestBody Map<String,Object> payload) {
        var commissionRate = (Integer) payload.get("commissionRate");
        if(commissionRate == null){
            commissionRate=0;
        }
        String userId = userContext.currentUserId();
        UpdateWrapper<DistributionMoneyLevel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id)
                .set("updater", userId)
                .set("commission_rate", commissionRate/100.0);
        return moneyLevelService.update(updateWrapper);
    }

}
